/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * TblUserLogicImpl.java, May 28, 2012 HaiPD
 */
package net.luvina.manageuser.logic.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.luvina.manageuser.dao.TblDetailUserJapanDao;
import net.luvina.manageuser.dao.TblUserDao;
import net.luvina.manageuser.dto.UserDetailDto;
import net.luvina.manageuser.hibernate.TblDetailUserJapan;
import net.luvina.manageuser.hibernate.TblUser;
import net.luvina.manageuser.logic.TblUserLogic;

/**
 * TblUserLogicImpl implements TblUserLogic
 * 
 * @author HaiPD
 * 
 */
@Service("userLogic")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = { Throwable.class })
public class TblUserLogicImpl implements TblUserLogic {

	@Autowired
	private TblUserDao tblUserDao;
	@Autowired
	private TblDetailUserJapanDao detailUserJapanDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.luvina.manageuser.logic.TblUserLogic#getListUser(int, int, int,
	 * java.lang.String, java.lang.String, java.lang.String, java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public List<UserDetailDto> getListUser(int offset, int limit, int groupId,
			String fullName, String sortType, String sortByFullName,
			String sortByCodeLevel, String sortByEndDate) {

		return this.tblUserDao.getListUser(offset, limit, groupId, fullName,
				sortType, sortByFullName, sortByCodeLevel, sortByEndDate);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.luvina.manageuser.logic.TblUserLogic#getTotalRecords(int,
	 * java.lang.String)
	 */
	@Override
	public int getTotalRecords(int groupId, String fullName) {
		return tblUserDao.getTotalRecords(groupId, fullName);
	}

	@Override
	public boolean checkExistedLoginName(int userId, String loginName) {
		TblUser tblUser = tblUserDao.checkExistedLoginName(userId, loginName);
		if (tblUser == null) {
			return false;
		}
		return true;
	}

	@Override
	public boolean checkExistedEmail(int userId, String email) {
		TblUser tblUser = tblUserDao.getUserByEmail(userId, email);
		if (tblUser == null) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.luvina.manageuser.logic.TblUserLogic#createUser(net.luvina.manageuser
	 * .hibernate.TblUser)
	 */
	@Override
	public boolean createUser(TblUser tblUser) {
		try {
			tblUserDao.create(tblUser);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.luvina.manageuser.logic.TblUserLogic#createDetailUserJapan(net.luvina
	 * .manageuser.hibernate.TblDetailUserJapan)
	 */
	@Override
	public boolean createDetailUserJapan(TblDetailUserJapan tblDetailUserJapan) {
		try {
			detailUserJapanDao.create(tblDetailUserJapan);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

}
